<?php
/**
 * 权限
 */
namespace app\model;

use think\Model;

class RoleMenu extends Model
{
   /**
    * 角色对应菜单列表
    *
    */
    public function GetMenu($roleid){
        $list = RoleMenu::where(['role_id'=> $roleid,'status'=>1])->column('menu_id');
        return  $list;
    }
  /**
   * 处理角色关系
   */
  public function DealRel($id,$ids=array()){
    $list = RoleMenu::where('role_id', $id)->select()->toArray();
    $TempId=[];
    foreach($list as $val){
        if(in_array($val['menu_id'],$ids)){
            $val['status']=1;
        }else{
            $val['status']=0;
        }
        RoleMenu::update($val);
        if(in_array($val['menu_id'],$ids)){
            foreach($ids as $key1=>$val1){
                if($val1==$val['menu_id']){
                   unset($ids[$key1]);
                   break;
                }
            }
        }    
    }

    if(!empty($ids)){
       $insert=[];
       foreach($ids as $key=>$val){
         if($val){
            $insert[]=['role_id'=>$id,'menu_id'=>$val,'status'=>1];
         }
       }
       $RoleMenu = new RoleMenu;
       $RoleMenu->saveAll($insert);
    }
  }
}